package com.exam.api.controller;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.exam.api.common.ReturnMsg;
import com.exam.api.dto.AdminDTO;
import com.exam.api.dto.StudentDTO;
import com.exam.api.dto.TeacherDTO;
import com.exam.api.service.AdminService;
import com.exam.api.service.StudentService;
import com.exam.api.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: 后台管理登录控制器
 * @author: wuming
 * @Create: 2024.05.03 13:24:03
 */
@RestController
@RequestMapping("/manage/login")
public class LoginController {

    static Map<String, String> userInfoMap = new HashMap<>();

    @Autowired
    private AdminService adminService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private StudentService studentService;


    @PostMapping
    public ReturnMsg manageLogin(@RequestBody Map<String, String> param) {
        if (StrUtil.isEmpty(param.get("username")) || StrUtil.isEmpty(param.get("password"))) {
            return ReturnMsg.error("用户名或密码错误!");
        }
        Map<String, Object> map = new HashMap<>();
        int type = Integer.parseInt(param.get("type").toString());
        // 管理员登录
        if (type == 0) {
            AdminDTO adminDTO = new AdminDTO();
            adminDTO.setUserName(param.get("username"));
            adminDTO.setPassWord(param.get("password"));
            QueryWrapper<AdminDTO> adminDTOQueryWrapper = new QueryWrapper<>(adminDTO);
            adminDTOQueryWrapper.last("limit 1");
            AdminDTO adminDTOS = adminService.getOne(adminDTOQueryWrapper);
            if (adminDTOS == null) return ReturnMsg.error("用户名或密码错误!");
            map.put("avatar", StrUtil.isNotBlank(adminDTOS.getHeadImg()) ? adminDTOS.getHeadImg() : "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
            map.put("introduction", adminDTOS.getNickName() + ",你好!");
            map.put("name", adminDTOS.getNickName());
            map.put("id", adminDTOS.getId());

        }
        // 老师登录
        else if (type == 1) {
            TeacherDTO teacherDTO = new TeacherDTO();
            teacherDTO.setUsername(param.get("username"));
            teacherDTO.setPassword(param.get("password"));
            QueryWrapper<TeacherDTO> adminDTOQueryWrapper = new QueryWrapper<>(teacherDTO);
            adminDTOQueryWrapper.last("limit 1");
            TeacherDTO adminDTOS = teacherService.getOne(adminDTOQueryWrapper);
            map.put("avatar", StrUtil.isNotBlank(adminDTOS.getAvatar()) ? adminDTOS.getAvatar() : "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
            map.put("introduction", adminDTOS.getName() + ",你好!");
            map.put("name", adminDTOS.getName());
            map.put("id", adminDTOS.getId());
        }
        // 学生登录
        else if (type == 2) {
            StudentDTO teacherDTO = new StudentDTO();
            teacherDTO.setUsername(param.get("username"));
            teacherDTO.setPassword(param.get("password"));
            QueryWrapper<StudentDTO> adminDTOQueryWrapper = new QueryWrapper<>(teacherDTO);
            adminDTOQueryWrapper.last("limit 1");
            StudentDTO adminDTOS = studentService.getOne(adminDTOQueryWrapper);
            map.put("avatar", StrUtil.isNotBlank(adminDTOS.getAvatar()) ? adminDTOS.getAvatar() : "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
            map.put("introduction", adminDTOS.getName() + ",你好!");
            map.put("name", adminDTOS.getName());
            map.put("id", adminDTOS.getId());
        }

        map.put("roles", Arrays.asList("admin"));
        map.put("type", type);
        Map<String, String> returnMap = new HashMap<>();
        String uuid = UUID.randomUUID().toString();
        returnMap.put("token", uuid);
        userInfoMap.put(uuid, JSON.toJSONString(map));
        return ReturnMsg.ok(returnMap);
    }

    @GetMapping
    public ReturnMsg getUserInfo(@RequestParam("token") String token) {
        if (!userInfoMap.containsKey(token)) return ReturnMsg.error(401, "尚未登录,请重新登录!");
        return ReturnMsg.ok(JSON.parse(userInfoMap.get(token)));
    }

    @GetMapping("/logout")
    public ReturnMsg logout(@RequestParam("token") String token) {
        userInfoMap.remove(token);
        return ReturnMsg.ok();
    }

}
